class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> ret;
        int m = s.size();
        int n = p.size();
        
        if(m < n) return {};
        
        vector<int> hash1(26),hash2(26);
        for(int i = 0;i < n;i++){
            hash1[s[i]-'a']++;
            hash2[p[i]-'a']++;
        }
        if(hash1 == hash2){
            ret.push_back(0);
        }
        
        int l = 0,r = n;
        while(r < m){
            hash1[s[r]-'a']++;
            hash1[s[l]-'a']--;
            r++;
            l++;
            
            if(hash1 == hash2){
                ret.push_back(l);
            }
        }
        return ret;
    }
};

